package zl_service

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/goravel/framework/support/carbon"
	"io/ioutil"
	"net/http"
)

// 订单流水列表
const (
	ORDER_LIST_URL = "/zl.open.api.oms.reportforms.querybilldatailforreport.v5/1.0.0/action"
)

type OrderListParam struct {
	AppKey    string `json:"appkey"`
	Orderno   string `json:"orderno"`
	Signtime  string `json:"signtime"`
	Starttime string `json:"starttime"`
	Endtime   string `json:"endtime"`
}
type OrderListService struct {
}

// 当天的订单列表
func (b *OrderListService) ListOrder() (error, any) {
	url := BASE_URL + ORDER_LIST_URL
	client := &http.Client{}

	orderParams := OrderListParam{
		AppKey:    APP_KEY,
		Orderno:   "fc0da34dd22140a79579a0740ed07f74",
		Signtime:  carbon.NewDateTime(carbon.Now()).Format("Y-m-d H:i:s"),
		Starttime: carbon.NewDateTime(carbon.Now()).StartOfDay().Format("Y-m-d H:i:s"),
		Endtime:   carbon.NewDateTime(carbon.Now()).EndOfDay().Format("Y-m-d H:i:s"),
	}

	bodyBytes, _ := json.Marshal(orderParams)
	stringToSign := fmt.Sprintf("%s&appsecret=%s", string(bodyBytes), APP_SECRET)
	upperSign := ComputeMD5AndToUpper(stringToSign)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyBytes))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Request-Sign", upperSign)
	body, err := client.Do(req)
	if err != nil {
		return err, nil
	}
	//将body
	res, err := ioutil.ReadAll(body.Body)
	//json序列化res
	if err != nil {
		return err, nil
	}
	var result map[string]interface{}
	err = json.Unmarshal(res, &result)
	return nil, result
}
